package com.javalong.affairs.module.renwujinzhang.ui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.debby.MyFileManager;
import com.javalong.affairs.R;
import com.javalong.affairs.base.common.ISynCallback;
import com.javalong.affairs.base.common.ServiceLocator;
import com.javalong.affairs.base.common.UIHandler;
import com.javalong.affairs.base.common.UIStatus;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.RequestCode;
import com.javalong.affairs.base.manager.IntentManager;
import com.javalong.affairs.base.model.User;
import com.javalong.affairs.base.utils.DateTimePickDialogUtil;
import com.javalong.affairs.base.utils.DateUtils;
import com.javalong.affairs.base.utils.IntUtils;
import com.javalong.affairs.base.utils.ToastUtils;
import com.javalong.affairs.module.notifything.activity.NewReceiverActivity;

import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * Created by 杨观回 on 2016/2/13 0013.
 */
public class SelectFileView extends FrameLayout implements ISynCallback, UIValue {
    private List<User> users;
    private ImageView iv_left;
    private TextView tv_file;
    private Button bt_select;
    private UIHandler uiHandler;

    public SelectFileView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SelectFileView(Context context) {
        super(context);
        init();
    }

    private int flag;

    private void init() {
        uiHandler = ServiceLocator.GetInstance().getInstance(UIHandler.class);
        flag = IntUtils.getNextInt();
        Date nowDate = new Date();
        LayoutInflater.from(getContext()).inflate(R.layout.item_select_file, this);
        tv_file = (TextView) findViewById(R.id.tv_file);
        iv_left = (ImageView) findViewById(R.id.iv_left);
        bt_select = (Button) findViewById(R.id.bt_select);
        bt_select.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt(BundleKey.FLAG, flag);
                IntentManager.doActivityForResult((Activity) getContext(),
                        MyFileManager.class, RequestCode.ADD_FILE, bundle);
            }
        });
        ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        setLayoutParams(param);
    }

    public void initData(String file) {
        tv_file.setText(file);
    }

    public void setHintText(String text) {
        tv_file.setText(text);
    }

    public void setStatus(UIStatus uiStatus) {
        switch (uiStatus) {
            case WATCH:
                bt_select.setVisibility(View.GONE);
                break;
        }
    }

    private File currFile;

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        uiHandler.registerCallback(this, flag);
    }

    @Override
    public void OnSynCalled(int ulID, Bundle bundle) {
        String file = bundle.getString(BundleKey.FILE);
        currFile = new File(file);
        if (!currFile.exists() || currFile.isDirectory()) {
            currFile = null;
            ToastUtils.showMessage("上传文件类型错误");
        } else {
            initData(file);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        uiHandler.unRegisterCallback(flag);
    }

    @Override
    public Object getValue() {
        return currFile;
    }

}
